let id = 0;
// 每个属性都分配一个dep  dep 可以存放watcher  watcher中还要存放dep
class Dep {
  constructor() {
    this.id = id++;
    this.subs = []; // 用来存放watacher
  }

  depend() {
    // Dep.target dep要存放这个watcher  watcher也要存放dep  多对多关系
    if (Dep.target) {
      Dep.target.addDep(this);
    }
  }

  addSub(watcher) {
    this.subs.push(watcher);
  }

  notify() {
    this.subs.forEach((watcher) => watcher.update());
  }
}

// 所有页面都同享一份
Dep.target = null;
let stack = [];
export function pushTarget(watcher) {
  Dep.target = watcher;
  stack.push(watcher);
}

export function popTarget() {
  stack.pop();
  Dep.target = stack[stack.push - 1];
}

export default Dep;
